<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############




JFX::checkAdminPermsRedirect('core', 'perm_edit_page');

$THEME->addBreadcrumb($this->adminUrl.'/edit-page/', $this->lang('ap_title_edit-page'));

$VIEW->template = 'admin-edit-page.tpl';

$VIEW->addJsLink($CONFIG->baseUrl.'/jfx/jfx/page-width-slider.js');


$templateid = $DB->escape(get('id'));
$workspaceid = $SETTINGS->get('live_workspace_id');

if(0 === (int) $DB->countRows($CONFIG->dbprefix.'blocks', "page_id = '{$SETTINGS->get('template_page_id')}' AND id = '{$templateid}' AND is_template = 1")){
    //JFX::addError('Please select a template to edit');
    JFX::redirect($CONFIG->adminUrl.'/templates/list/');
}


$pageid = $SETTINGS->get('template_page_id');

$SMARTY->assign('currentPageId', $pageid);
$SMARTY->assign('currentPageTitle', $LANG->getContent('core', 'heading', $pageid));

JFX::checkAdminPermsRedirect('core', 'perm_page_'.$workspaceid.'_'.$pageid);

$PAGE = JFX_Block::getBlockById($pageid, $pageid);
$SMARTY->assign('PAGE', $PAGE->get());

$pageButtons = $THEME->getPageButtons($pageid);
$SMARTY->assign('pageButtons', $pageButtons);


$templates = array();
$block = new JFX_Block;
$tps = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE page_id = '{$pageid}' AND is_template = 1 AND workspace_id = '{$workspaceid}' AND page_parent = 1");
foreach($tps as $k=>$v){
    $block->loadById($v['id'], $pageid, $workspaceid, false, true);
    $tps[$k]['title'] = JFX_Block::getTitleById($block->id, '', $workspaceid);
}


// sort by title
$newtps = array();
foreach($tps as $v){
    $newtps[$v['title']] = $v;
}

ksort($newtps);
foreach($newtps as $v){
    $templates[] = array(
        'title' => $v['title'],
        'id' => $v['id']
    );
}


$modules = array();
$mods = JFX_Module::getInstalledModules();
$count = 0;
foreach($mods as $k=>$v){
    $module = JFX::registry('JFX_Module_'.$v['keyname']);
    $actions = $module->getBlockActions();
    if(!is_array($actions) || count($actions)==0) continue;
    $modules[$count] = array(
        'keyname' => $module->keyname,
        'title' => $module->getTitle(),
        'actions' => array()
    );
    foreach($actions as $key=>$value){
        $modules[$count]['actions'][] = array(
            'title' => $value['title'],
            'keyname' => $value['keyname']
            //'description' => trim((string) $value['description'])
        );
    }
    $count++;
}


$SMARTY->assign('insertMenu', '<ul class="insert-menu"></ul>');

$SMARTY->assign('canEditPageDetails', false);
$SMARTY->assign('showPageWidthSlider', true);



echo '<div class="jfx-droppable jfx-page" id="'.$pageid.'"></div>';

$eParams = array(
    'page_id' => $pageid,
    'template_id' => $templateid,
	'workspace_id' => $workspaceid
);




JFX::hook('JFX_Core.editTemplateLoad', $eParams);
